import axios from 'axios';

// 基础API URL
const API_URL = process.env.REACT_APP_API_URL || 'http://localhost:8080/api';

// 创建axios实例
const apiClient = axios.create({
  baseURL: API_URL,
  headers: {
    'Content-Type': 'application/json'
  }
});

// 拦截器添加token到请求头
apiClient.interceptors.request.use(
  config => {
    const token = localStorage.getItem('token');
    if (token) {
      config.headers['Authorization'] = `Bearer ${token}`;
    }
    return config;
  },
  error => {
    return Promise.reject(error);
  }
);

// 电影服务对象
const MovieService = {
  // 获取所有电影
  getMovies: () => {
    // 目前使用模拟数据，后续连接后端时取消注释下面代码
    // return apiClient.get('/movies');
    
    // 返回模拟数据
    return new Promise((resolve) => {
      setTimeout(() => {
        resolve({
          data: mockMovies
        });
      }, 500);
    });
  },
  
  // 获取单个电影详情
  getMovie: (id) => {
    // return apiClient.get(`/movies/${id}`);
    
    // 返回模拟数据
    return new Promise((resolve) => {
      setTimeout(() => {
        const movie = mockMovies.find(m => m.id === parseInt(id)) || null;
        resolve({
          data: movie
        });
      }, 500);
    });
  },
  
  // 获取电影的放映场次
  getScreenings: (movieId) => {
    // return apiClient.get(`/movies/${movieId}/screenings`);
    
    // 返回模拟数据
    return new Promise((resolve) => {
      setTimeout(() => {
        resolve({
          data: mockScreenings.filter(s => s.movieId === parseInt(movieId))
        });
      }, 500);
    });
  },
  
  // 获取场次的座位信息
  getSeats: (screeningId) => {
    // return apiClient.get(`/screenings/${screeningId}/seats`);
    
    // 模拟数据 - 生成座位数据
    return new Promise((resolve) => {
      setTimeout(() => {
        const rows = 10;
        const cols = 15;
        const seats = [];
        
        for (let i = 0; i < rows; i++) {
          const row = [];
          for (let j = 0; j < cols; j++) {
            row.push({
              id: `${i}-${j}`,
              row: i + 1,
              col: j + 1,
              status: Math.random() > 0.2 ? 'available' : 'occupied'
            });
          }
          seats.push(row);
        }
        
        resolve({
          data: seats
        });
      }, 500);
    });
  },
  
  // 提交订单
  submitOrder: (orderData) => {
    // return apiClient.post('/orders', orderData);
    
    // 模拟订单提交
    return new Promise((resolve) => {
      setTimeout(() => {
        resolve({
          data: {
            orderId: 'ORD' + Math.floor(Math.random() * 1000000),
            status: 'PENDING',
            message: '订单创建成功，等待支付'
          }
        });
      }, 800);
    });
  }
};

// 模拟数据 - 电影列表
const mockMovies = [
  {
    id: 1,
    title: '流浪地球2',
    originalTitle: 'The Wandering Earth 2',
    posterUrl: 'https://n.sinaimg.cn/sinakd20230123s/706/w1906h2800/20230123/9137-b383634783de89cde4d98c83c618be19.jpg',
    backdropUrl: 'https://img2.doubanio.com/view/photo/l/public/p2885955777.jpg',
    releaseDate: '2023-01-22',
    durationMinutes: 173,
    director: '郭帆',
    cast: '吴京, 刘德华, 李雪健, 沙溢, 宁理',
    synopsis: '太阳即将毁灭，人类在地球表面建造出巨大的推进器，寻找新家园。然而宇宙之路危机四伏，为了拯救地球，流浪地球时代的年轻人再次挺身而出，展开争分夺秒的生死之战。',
    language: '中文',
    country: '中国',
    rating: 8.3,
    status: 'RELEASED',
    categories: ['科幻', '冒险', '灾难']
  },
  {
    id: 2,
    title: '满江红',
    originalTitle: 'Full River Red',
    posterUrl: 'https://n.sinaimg.cn/ent/transform/523/w550h773/20230124/b88e-f30786ed496b8f5fca1db60b98b708ee.jpg',
    backdropUrl: 'https://img9.doubanio.com/view/photo/l/public/p2886465517.jpg',
    releaseDate: '2023-01-22',
    durationMinutes: 159,
    director: '张艺谋',
    cast: '沈腾, 易烊千玺, 张译, 雷佳音, 岳云鹏',
    synopsis: '南宋绍兴年间，金国使者来宋，要求宋朝割让土地，遭到北方士兵张大的反对，为此他绑架了金国使者，引起了两国的外交矛盾。',
    language: '中文',
    country: '中国',
    rating: 7.5,
    status: 'RELEASED',
    categories: ['犯罪', '悬疑', '古装']
  }
];

// 模拟数据 - 放映场次
const mockScreenings = [
  {
    id: 1,
    movieId: 1,
    startTime: '2023-05-15 10:00:00',
    endTime: '2023-05-15 12:00:00',
    price: 35.0,
    hallName: '1号厅',
    cinemaName: '星光影院',
    availableSeats: 80
  },
  {
    id: 2,
    movieId: 1,
    startTime: '2023-05-15 13:00:00',
    endTime: '2023-05-15 15:00:00',
    price: 35.0,
    hallName: '2号厅',
    cinemaName: '星光影院',
    availableSeats: 65
  },
  {
    id: 3,
    movieId: 1,
    startTime: '2023-05-15 16:00:00',
    endTime: '2023-05-15 18:00:00',
    price: 45.0,
    hallName: '1号厅',
    cinemaName: '环球影院',
    availableSeats: 50
  },
  {
    id: 4,
    movieId: 1,
    startTime: '2023-05-15 19:00:00',
    endTime: '2023-05-15 21:00:00',
    price: 45.0,
    hallName: 'IMAX厅',
    cinemaName: '环球影院',
    availableSeats: 25
  },
  {
    id: 5,
    movieId: 2,
    startTime: '2023-05-15 11:00:00',
    endTime: '2023-05-15 13:00:00',
    price: 35.0,
    hallName: '3号厅',
    cinemaName: '星光影院',
    availableSeats: 70
  },
  {
    id: 6,
    movieId: 2,
    startTime: '2023-05-15 14:30:00',
    endTime: '2023-05-15 16:30:00',
    price: 40.0,
    hallName: '4号厅',
    cinemaName: '环球影院',
    availableSeats: 85
  }
];

export default MovieService; 